package com.peter.alertdialog_k

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity

var a: Int = 3

fun hello(): Int {
    return 1
}




class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mContext = this@MainActivity
        findViewById<View>(R.id.bt).setOnClickListener(this)
        findViewById<View>(R.id.bt1).setOnClickListener(this)
        findViewById<View>(R.id.bt2).setOnClickListener(this)
        findViewById<View>(R.id.bt3).setOnClickListener(this)
    }

     var a :Int ? = null

    private var alert: AlertDialog? = null
    private var builder: AlertDialog.Builder? = null
    private var mContext: Context? = null
    private lateinit var checkItems: BooleanArray
    override fun onClick(v: View) {
        when (v.id) {
            R.id.bt -> {
                alert = null
                builder = AlertDialog.Builder(mContext!!)
                alert = builder!!.setIcon(R.mipmap.ic_launcher)
                    .setTitle("系统提示：")
                    .setMessage("这是一个最普通的AlertDialog,\n带有三个按钮，分别是取消，中立和确定")
                    .setNegativeButton(
                        "取消"
                    ) { dialog, which ->
                        Toast.makeText(mContext, "你点击了取消按钮~", Toast.LENGTH_SHORT).show()
                    }
                    .setPositiveButton(
                        "确定"
                    ) { dialog, which ->
                        Toast.makeText(mContext, "你点击了确定按钮~", Toast.LENGTH_SHORT).show()
                    }
                    .setNeutralButton(
                        "中立"
                    ) { dialog, which ->
                        Toast.makeText(mContext, "你点击了中立按钮~", Toast.LENGTH_SHORT).show()
                    }.create() //创建AlertDialog对象
                alert!!.show() //显示对话框
            }
            R.id.bt1 -> {
                val fruits = arrayOf("苹果", "雪梨", "香蕉", "葡萄", "西瓜")
                alert = null
                builder = AlertDialog.Builder(mContext!!)
                alert = builder!!.setIcon(R.mipmap.ic_launcher)
                    .setTitle("选择你喜欢的水果，只能选一个哦~")
                    .setSingleChoiceItems(
                        fruits, 0
                    ) { dialog, which ->
                        Toast.makeText(
                            applicationContext,
                            "你选择了" + fruits[which],
                            Toast.LENGTH_SHORT
                        ).show()
                    }.create()
                alert!!.show()
            }
            R.id.bt2 -> {
                val menu = arrayOf("水煮豆腐", "萝卜牛腩", "酱油鸡", "胡椒猪肚鸡")
                //定义一个用来记录个列表项状态的boolean数组
                checkItems = booleanArrayOf(false, false, false, false)
                alert = null
                builder = AlertDialog.Builder(mContext!!)
                alert = builder!!.setIcon(R.mipmap.ic_launcher)
                    .setMultiChoiceItems(
                        menu, checkItems
                    ) { dialog, which, isChecked -> checkItems[which] = isChecked }
                    .setPositiveButton(
                        "确定"
                    ) { dialog, which ->
                        var result = ""
                        var i = 0
                        while (i < checkItems.size) {
                            if (checkItems[i]) result += menu[i] + " "
                            i++
                        }
                        Toast.makeText(applicationContext, "客官你点了:$result", Toast.LENGTH_SHORT)
                            .show()
                    }
                    .create()
                alert!!.show()
            }
            R.id.bt3 -> Log.e("Msg----", "AAAAAA")
        }
    }
}